package com.cz.mes;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class PuChangProductionMaterial {

    public static void main(String[] args) {
        String dbGroupName = "mes-prod-master";
        List<String> moczCodesList2 = Arrays.asList(
                "MOTJ2506250020"
        );
        for (String moczCode : moczCodesList2) {
            try {
                Entity mocEntity = Db.use(dbGroupName).queryOne("select * from manufacture_order where bill_no =?", moczCode);
                int id = mocEntity.getInt("id");
                String organization = mocEntity.getStr("organization");
                String bill_type = mocEntity.getStr("bill_type");
                String bill_no = mocEntity.getStr("bill_no");
                String type = mocEntity.getStr("type");

                List<Entity> mocDetailEntity = Db.use(dbGroupName).query("select * from manufacture_order_detail where main_id =?", id);
                mocDetailEntity.forEach(entity -> {
                    String skuCode = entity.getStr("sku_code");
                    String skuName = entity.getStr("sku_name");
                    String specification_model = entity.getStr("specification");
                    String workShopCode = entity.getStr("work_shop_code");
                    String unit = entity.getStr("unit");
                    BigDecimal quantity = entity.getBigDecimal("quantity");
                    int line_no = entity.getInt("line_no");
                    String material_attr = entity.getStr("material_attr");
                    String material_group = entity.getStr("material_group");
                    String sale_bill_no = entity.getStr("sale_bill_no");
                    int customer_id = entity.getInt("customer_id");
                    String customer_code = entity.getStr("customer_code");
                    String erp_customer_code = entity.getStr("erp_customer_code");
                    String customer_name = entity.getStr("customer_name");
                    Date demand_date = entity.getDate("demand_date");
                    Integer sales_person = entity.getInt("sales_person");
                    String sales_department_name = entity.getStr("sales_department_name");
                    String product_storage = entity.getStr("product_storage");
                    String moc_detail_id = entity.getStr("id");
                    Integer kingdee_data = entity.getInt("kingdee_data");
                    String bom_version = entity.getStr("bom_version");

                    String material_bill_no = "POM" + moczCode.substring(2, moczCode.length()) + RandomUtil.randomString(6);

                    System.out.println(material_bill_no);

                    Entity entity1 = new Entity("production_material");
                    entity1.set("bill_no", material_bill_no);
                    entity1.set("sku_code", skuCode);
                    entity1.set("sku_name", skuName);
                    entity1.set("specification_model", specification_model);
                    entity1.set("bom_version", bom_version);
                    entity1.set("production_workshop", workShopCode);
                    entity1.set("product_organization", organization);
                    entity1.set("unit", unit);
                    entity1.set("quantity", quantity);
                    entity1.set("production_order_type", bill_type);
                    entity1.set("moc_no", bill_no);
                    entity1.set("production_order_line_number", line_no);
                    entity1.set("sales_order_no", sale_bill_no);
                    entity1.set("customer_id", customer_id);
                    entity1.set("customer_code", customer_code);
                    entity1.set("erp_customer_code", erp_customer_code);
                    entity1.set("customer_name", customer_name);
                    entity1.set("delivery_date", demand_date);
                    entity1.set("salesperson", sales_person);
                    entity1.set("sales_department", sales_department_name);
                    entity1.set("production_or_storage_transportation", product_storage);
                    entity1.set("status", 1);
                    entity1.set("moc_detail_id", moc_detail_id);
                    entity1.set("type", type);
                    entity1.set("kingdee_data", kingdee_data);
                    entity1.set("parent_material_attribute", material_attr);
                    entity1.set("parent_material_grouping", material_group);

                    System.out.println(sale_bill_no);
                    try {

                        int count = Db.use(dbGroupName).insert(entity1);
                        System.out.println("生产物料入库" + count + "<UNK>");

                        String sql = "update manufacture_order_detail set p_bom_no =? where id =?";
                        Db.use(dbGroupName).execute(sql,material_bill_no,moc_detail_id);
                        System.out.println(material_bill_no + " "+ moc_detail_id + "更新bom_no成功");
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                });

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}
